package com.tuling.fanout_exchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 扇形交换机的
 * 如果mq服务器不存在该交换机这些，启动会报错。
 * 此示例是在消费端声明了，声明就是创建了。
 * 手动在服务器上创建也行，在代码里声明也行，无所谓在哪声明，看实际场景。
 * 只是创建了一下而已。且同名的只能创建一次。
 */
public class FanoutExchangeProductor {

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.36.32");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("wxm");
        connectionFactory.setUsername("quotacenter");
        connectionFactory.setPassword("quotacenter");

        //创建连接
        Connection connection = connectionFactory.newConnection();

        //创建channel
        Channel channel = connection.createChannel();

        //定义交换机名称
        String exchangeName = "tl.fanoutexchange";

        //定义routingKey
        String routingKey = "";

        //消息体内容
        String messageBody = "hello tuling ";
        for(int i=0;i<1000;i++) {

            channel.basicPublish(exchangeName,"123",null,(messageBody+i).getBytes());
        }

        /*channel.basicPublish(exchangeName,"456",null,"我是第二条消息".getBytes());
        channel.basicPublish(exchangeName,"789",null,"我是第三条消息".getBytes());*/
    }
}
